<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Comment extends CI_Controller {

	public function index(){
		$code = $this->input->get('code');
		$url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=wxe65fd69d2da5b545&secret=4be05eba35d47feb16184dc101361ea0&code=$code&grant_type=authorization_code";
		$json_info = json_decode(https_request($url),true);
		$access_token = $json_info['access_token'];
		$openid = $json_info['openid'];
		$user_url = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN";
		$userData = json_decode(https_request($user_url),true);
		$nickname = $userData['nickname'];
		

		header("location:http://mysise.duapp.com/index.php/comment/commentview/?name=".$nickname."&img=".$userData['headimgurl']);
		
		
		
	}

	public function commentview(){
		$name = $this->input->get('name');
		$img = $this->input->get('img');
		// $comment = $this->student_model->getcomment();
		$number = $this->input->get('per_page');
		$this->load->library('pagination');
		$perPage = 7;
		$config['base_url'] = site_url("comment/commentview/?name=$name&img=$img");
		$config['total_rows'] = $this->db->count_all_results('comment');
		//每页显示多少条
		$config['per_page'] = $perPage;
		$config['enable_query_strings'] = TRUE;
		$config['page_query_string'] = TRUE;
		$config['query_string_segment'] = 'per_page';
		$config['full_tag_open'] = '<ul class="pagination pagination-sm">';
		$config['full_tag_close'] = '</ul>';
		$config['cur_tag_open'] = '<li class="am-active"><a>';
		$config['cur_tag_close'] = '</a></li>';
		$config['prev_tag_open'] = '<li>';
		$config['prev_tag_close'] = '</li>';
		$config['num_tag_open'] = '<li>';
		$config['num_tag_close'] = '</li>';
		$config['next_tag_open'] = '<li>';
		$config['next_tag_close'] = '</li>';
		$config['last_link'] = FALSE;
		$config['first_link'] = FALSE;

 		$this->pagination->initialize($config);
 		$data = array(
 				'comment' => $this->student_model->get_pagination('comment',$config['per_page'],$number),
 				'links' =>$this->pagination->create_links(),
 				'nickname' => $name,
				'headimgurl' => $img
				
 			);
		// $data = array(
		// 	'nickname' => $name,
		// 	'headimgurl' => $img,
		// 	'comment' => $comment
		// 	);
		$this->load->view('comment',$data);
	}


	public function post_comment(){
		$nickname = $this->input->post('nickname');
		$headimgurl = $this->input->post('headimgurl');
		$content = $this->input->post('content');
		$data = array(
			'content' => $content,
			'nickname' => $nickname,
			'headimgurl' => $headimgurl
			);
		$something = $this->student_model->comment_insert($data);
		echo $something;
		
	}
}